﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace _0712502_0712504_DesignPatternGame
{
    public class PlayingGame
    {
        public bool _isFinished = false;
        public bool _isGameOver = false;

        public int _returnValue = -1;
        public int _endPlayingValue = -1;
        public int timeDelayToEnding = 0;   // Thời gian delay khi kết thúc game
       
      
        private MyMap mMap;
        private GameDisplayDrawer gameDisplayDrawer = new GameDisplayDrawer();
        private AutomatedTankManager _comTankManager;
        public AutomatedTankManager ComTankManager
        {
            get { return _comTankManager; }
            set { _comTankManager = value; }
        }

        private UserControlledTankManager _playerTankManager;
        public UserControlledTankManager PlayerTankManager
        {
            get { return _playerTankManager; }
            set { _playerTankManager = value; }
        }

        private ProductManager _itemManager;

        public ProductManager ItemManager
        {
            get { return _itemManager; }
            set { _itemManager = value; }
        }

        public PlayingGame()
        {
            mMap = new MyMap();            

         //   comTank = new ComTank1();
            this.PlayerTankManager = new UserControlledTankManager();
            this.ComTankManager = new AutomatedTankManager();
            this.ItemManager = new ProductManager();
        }

        public void Initilize()
        {
            this.ComTankManager.Intitilize();
        }

        public void LoadNewStage(ref MyMap map)
        {
            mMap = map;          
            this.ComTankManager._isFinished= false;
            InitPlayerTank();
           
           // _products  = new Products();
            this.ComTankManager.InitilizeForNewStage();
            MyMap.isFinished = false;
            this._returnValue = -1;
            this._endPlayingValue = -1;

            this.ComTankManager.AddNewTankToGameStage();
            gameDisplayDrawer.Initilize();            
        }


        public void Update(GameTime gameTime)
        {
            // Delay time trc khi kết thúc game
            if (_returnValue == 2)
            {
                timeDelayToEnding++;
                if (timeDelayToEnding > 120)
                {
                    this._isFinished = true;
                }
                
            }

            this.ProcessKeyBoard();

            if (_isFinished == false)
            {
                mMap.Update();
                if (_isGameOver == false)
                {                  
                    PlayerTankManager.Update(gameTime);
                }

                ComTankManager.Update(gameTime);
                
                if (this.PlayerTankManager.IsFinished == true || MyMap.isFinished == true)  // Game over
                {
                    //MediaPlayer.Stop();
                    _isGameOver = true;
                    //ProcessGameOver();
                    _returnValue = 2;   // End game
                    _endPlayingValue = 2;
                }

                if (this.ComTankManager._isFinished == true)  // Đã tiêu diệt hết tank
                {
                    //MediaPlayer.Stop();
                    _returnValue = 2;   // End game
                    _endPlayingValue = 2;
                }                                               
            }
        }

        public void ProcessKeyBoard()   // Xu ly nhan phim khi dang choi
        {

        }

        public void ProcessGameOver()
        {

        }

        public void DrawToScreen(GameTime gameTime, SpriteBatch spriteBatch)
        {

        //   mMap.DrawMapBackGround(gameTime, spriteBatch);
          //  mMap.DrawRightTopMapBorder(gameTime, spriteBatch);
            mMap.DrawMapTerrain(gameTime, spriteBatch);
            PlayerTankManager.DrawToScreen(gameTime, spriteBatch);
            //mMap.DrawLeftBottomMapBorder(gameTime, spriteBatch);
            mMap.DrawLandScape(gameTime, spriteBatch);
     
            ComTankManager.DrawToScreen(gameTime, spriteBatch);
            gameDisplayDrawer.DrawToScreen(gameTime, spriteBatch);
        }

        public void InitPlayerTank()
        {        
            this.PlayerTankManager.Initilize();
        }
    }
}
